/*
 * Copyright (C) 2008 Wim Crols <twimpiex@gnx.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package be.gnx.event.consumer.application.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import be.gnx.event.consumer.application.action.Exit;
import be.gnx.event.consumer.application.configuration.Configuration;
import be.gnx.event.consumer.application.event.LoggingEntriesEvents;
import be.gnx.event.consumer.application.ui.widget.EventEntriesWidget;
import be.gnx.event.consumer.application.ui.widget.EventEntryWidget;
import be.gnx.event.consumer.receiver.Receiver;
import be.gnx.event.consumer.receiver.model.Event;
import be.gnx.event.consumer.receiver.net.SocketListener;

public class EventConsumer implements LoggingEntriesEvents  {

  private Shell shell;

  private StatusBar statusBar;

  private EventEntriesWidget eventEntriesWidget;

  private EventEntryWidget eventEntryWidget;

  public EventConsumer(Shell shell) {
    this.shell = shell;
    new MenuBar(this, shell);
    statusBar = new StatusBar(this, shell);
    eventEntriesWidget = new EventEntriesWidget(shell, SWT.NONE);

    for (Receiver receiver : Configuration.getReceivers()) {
    	receiver.getEventProcessor().addEventProcessorEvents(eventEntriesWidget);
    	receiver.start(true);
    }

    eventEntryWidget = new EventEntryWidget(shell, SWT.NONE);
    eventEntriesWidget.addLoggingEntriesEvents(this);

    shell.addListener(SWT.Close, new Exit(this));

    resize();
  }

  public void selectedLoggingEvent(Event event) {
    eventEntryWidget.setLoggingEvent(event);
  }

  public void resize() {
    statusBar.resize();
    int statusBarHeight = statusBar.getHeight();
    eventEntriesWidget.setBounds(0, 0, shell.getClientArea().width, (shell.getClientArea().height - statusBarHeight) / 2);
    eventEntryWidget.setBounds(0, (shell.getClientArea().height - statusBarHeight) / 2, shell.getClientArea().width, (shell.getClientArea().height - statusBarHeight) / 2);
  }

  public StatusBar getStatusBar() {
    return statusBar;
  }

  public static void main(String[] args) {
    Display.setAppName("EventConsumer");
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setMaximized(true);
    shell.setText("EventConsumer");
    //shell.setImage(null); // this is to set an icon, does not work with SWTSwing

    final EventConsumer eventConsumer = new EventConsumer(shell);
    shell.addControlListener(new ControlAdapter() {
      public void controlResized(ControlEvent controlEvent) {
        eventConsumer.resize();
      }
    });

    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }

}
